// pages/index/zone/goodsDetail/mergeorder/mergeorder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //蒙层
    maskstate:false,
    mainboxshow:'',
    mergeboxshow:'',
    //此拼单商品信息
    mergedata:'',
    //拼单价
    markupPrice:'',
    //拼单商品最大购买数量
    buyupnum:'',
    //拼单商品是否选中
    mergerchecked: true,
    //上一页的全部数据
    prevPagedata:'',
    //主商品选中的颜色
    colorchecked:'',
    //主商品选中的尺码
    skuchecked:'',
    //上一页主商品的颜色表
    targetColor:[],
    //上一页主商品的尺码表
    targetSkusize:[],
    //上一页主商品的规格id数组
    specData:[],
    //上一页主商品选择的数量
    nowstock: 1,
    //上一页主商品选中规格商品的库存
    checkedstock: '',
    //上一页主商品选中规格的id
    specId: '',
    //拼单商品选中的颜色
    mergecolorchecked: '绿色',
    //拼单商品选中的尺码
    mergeskuchecked: "M",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];  // 获取上一页实例   可以获取上一页的数据并改变
    console.log(prevPage.data)
    this.setData({
      mergedata: prevPage.data.collage[options.index],  //上一页选中的拼单数据
      markupPrice: parseFloat(prevPage.data.collage[options.index].markupPrice),  //上一页选中的拼单数据的商品促销语
      buyupnum: prevPage.data.collage[options.index].buyupnum,   //上一页选中的拼单数据的限购人数
      prevPagedata: prevPage.data,                                //上一页主商品的全部数据
      colorchecked: prevPage.data.colorchecked,                   //上一页主商品选中的颜色
      skuchecked: prevPage.data.skuchecked,                        //上一页主商品选中的尺码
      targetColor: prevPage.data.targetColor,                       //上一页主商品的颜色表
      targetSkusize: prevPage.data.targetSkusize,                     //上一页主商品的尺码表
      specData: prevPage.data.specData,                              //上一页主商品的规格id数组
      nowstock: prevPage.data.nowstock,                //上一页主商品选择的数量
      checkedstock: prevPage.data.checkedstock,         //上一页主商品选中规格商品的库存
      specId: prevPage.data.specId,                     //上一页主商品选中规格的id

    })
    console.log(this.data.mergedata)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //打开主商品弹窗
  mainboxshow () {
    this.setData({
      mainboxshow: true,
      maskstate: true
    })
  },
  //关闭主商品弹窗
  mainboxhide () {
    this.setData({
      mainboxshow: false,
      maskstate: false
    })   
  },
  //打开拼单商品弹窗
  mergeboxshow() {
    this.setData({
      mergeboxshow: true,
      maskstate: true
    })
  },
  //关闭拼单商品弹窗
  mergeboxhide() {
    this.setData({
      mergeboxshow: false,
      maskstate: false
    })
  },
  //选中或者取消拼单商品
  mergecheck () {
    this.setData({
      mergerchecked: !this.data.mergerchecked
    })
  },
  //点击蒙层 关闭所有弹窗
  closemask () {
    this.setData({
      mainboxshow: false,
      mergeboxshow: false,
      maskstate: false
    })
  },
  //选择颜色
  choosecolor(e) {
    //重置用户选择数量
    this.setData({
      nowstock: 1
    })
    //如果可以购买 有库存
    if (e.currentTarget.dataset.cansell) {
      let obj = this.data.targetSkusize
      let curarr = this.data.targetColor
      let index = e.currentTarget.dataset.index
      let choosecolor = e.currentTarget.dataset.color
      if (curarr[index].checked) {
        curarr[index].checked = !curarr[index].checked
      } else {
        curarr.map(function (x) {
          x.checked = false
          return x.checked
        })
        curarr[index].checked = !curarr[index].checked
      }
      let result = curarr[index].checked ? this.data.specData.filter(x => x.color == choosecolor) : this.data.specData
      let colorbook = new Array(result.length)
      for (let i = 0; i < result.length; i++) {
        colorbook[i] = result[i].skusize
      }
      obj.forEach((value, index) => {
        if (!colorbook.includes(value.val)) {
          obj[index].cansell = false
        } else {
          obj[index].cansell = true
        }
      })
      this.setData({
        targetSkusize: obj,
        targetColor: curarr
      })
      console.log(curarr[index].val)
      this.setData({
        colorchecked: curarr[index].checked ? curarr[index].val : ''
      })
    }
  },
  //选择规格
  choosesku(e) {
    //重置用户选择数量
    this.setData({
      nowstock: 1
    })
    //如果可以购买 有库存
    if (e.currentTarget.dataset.cansell) {
      let obj = this.data.targetColor
      let curarr = this.data.targetSkusize
      let index = e.currentTarget.dataset.index
      let choosesku = e.currentTarget.dataset.skusize
      if (curarr[index].checked) {
        curarr[index].checked = !curarr[index].checked
      } else {
        curarr.map(function (x) {
          x.checked = false
          return x.checked
        })
        curarr[index].checked = !curarr[index].checked
      }
      let result = curarr[index].checked ? this.data.specData.filter(x => x.skusize == choosesku) : this.data.specData
      let skubook = new Array(result.length)
      for (let i = 0; i < result.length; i++) {
        skubook[i] = result[i].color
      }
      obj.forEach((value, index) => {
        if (!skubook.includes(value.val)) {
          obj[index].cansell = false
        } else {
          obj[index].cansell = true
        }
      })
      this.setData({
        targetColor: obj,
        targetSkusize: curarr
      })
      console.log(curarr[index].val)
      this.setData({
        skuchecked: curarr[index].checked ? curarr[index].val : ''
      })
    }
  },
  //加法
  add() {
    let nowstock = this.data.nowstock
    //如果未选择规格或者颜色
    if ((this.data.targetColor.length ? !this.data.colorchecked : true) || (this.data.targetSkusize.length ? !this.data.skuchecked : true) ) {
      wx.showToast({
        title: '请选择颜色或尺码',
        icon: "none"
      })
    } else {
      //获取选中规格的库存
      this.stockandspecid()
      if (nowstock < this.data.checkedstock) {
        nowstock++
        this.setData({
          nowstock: nowstock
        })
      } else {
        wx.showToast({
          title: '不能大于最大库存',
          icon: "none"
        })
      }
    }
  },
  //减法
  sub() {
    let nowstock = this.data.nowstock
    //如果未选择规格或者颜色
    if ((this.data.targetColor.length ? !this.data.colorchecked : true) || (this.data.targetSkusize.length ? !this.data.skuchecked : true) ) {
      wx.showToast({
        title: '请选择颜色或尺码',
        icon: "none"
      })
    } else {
      //获取选中规格的库存
      this.stockandspecid()
      if (nowstock > 1) {
        nowstock--
        this.setData({
          nowstock: nowstock
        })
      } else {
        wx.showToast({
          title: '一件起售',
          icon: "none"
        })
      }
    }
  },
  //计算选中颜色尺码下的库存和规格id
  stockandspecid() {
    let colorchecked = this.data.colorchecked
    let skuchecked = this.data.skuchecked
    let specData = this.data.specData
    let specId
    let checkedstock
    //如果只有颜色，查颜色
    if (colorchecked && !skuchecked) {
      specId = specData.find(x => x.color == colorchecked).specId
      checkedstock = specData.find(x => x.color == colorchecked).stock
      //如果只有尺码，查尺码
    } else if (skuchecked && !colorchecked) {
      specId = specData.find(x.skusize == skuchecked).specId
      checkedstock = specData.find(x.skusize == skuchecked).stock
      //颜色尺码都查
    } else {
      specId = specData.find(x => x.color == colorchecked && x.skusize == skuchecked).specId
      checkedstock = specData.find(x => x.color == colorchecked && x.skusize == skuchecked).stock
    }
    this.setData({
      specId: specId,
      checkedstock: checkedstock
    })
    console.log('specId:' + specId, 'checkedstock:' + checkedstock)
  }
})